package com.demo.controller;

import com.demo.domain.Dto;
import com.demo.domain.User;
import com.demo.service.UserService;
import com.demo.utils.DtoUtil;
import com.demo.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * (User)表控制层
 *
 * @author 李浩
 * @since 2021-06-30 08:49:39
 */
@RestController
@RequestMapping("api/user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }

    @PostMapping("login")
    public Dto login(@RequestBody LoginVo loginVo){
        String name = loginVo.getName();
        String pwd = loginVo.getPwd();
        if(name == null || name.equals("")){
            return DtoUtil
                    .returnError(503,"用户名未输入");
        }

        User user = userService.queryByName(name,pwd);

        if(user == null){
            return DtoUtil.returnError(504,"账号密码错误");
        }else{
            return DtoUtil.returnSuccessData(user);
        }

    }

}